# -*- coding: utf-8 -*-
import xadmin
from xadmin import views
from .models import BlogMsg, BlogType


class BaseSetting(object):
    enable_themes = True
    use_bootswatch = True


class GlobalSetting(object):
    site_title = u'xBlog后台管理系统'
    site_footer = u'xBlog个人博客系统'


class BlogMsgAdmin(object):
    list_display = ('title', 'blog_type', 'create_time')

    search_fields = ('title', 'blog_type__type')
    list_filter = ('title', 'create_time', 'blog_type')

    def save_models(self):
        """
        当新增博客时 更新博客类型统计数字
        :return:
        """

        obj = self.new_obj
        obj.save()

        blog_type = BlogType.objects.get(type_name=obj.blog_type)
        blog_type.count = BlogMsg.objects.filter(is_public=0, blog_type=obj.blog_type).count()
        blog_type.save()



class BlogTypeAdmin(object):
    list_display = ('type_name', 'count', 'crate_time')
    search_fields = ('type_name', )


xadmin.site.register(BlogMsg, BlogMsgAdmin)
xadmin.site.register(BlogType, BlogTypeAdmin)

xadmin.site.register(views.BaseAdminView, BaseSetting)
xadmin.site.register(views.CommAdminView, GlobalSetting)